﻿/**
 * @class E
 * @singleton
 */
(function(){
window.E = {
	ekey : 'ek' + new Date().getTime(),
	eid : 1,
	evtData : {
		/*
		1: {
		'swipLeft': function(e) {
		alert(this.innerHTML);
		}
		}
		 */
	},
	trigger : function (e, target, evtName) {
		if (!target)
			return false;
		var ek;
		while (target) {
			if (target.getAttribute)
				ek = target.getAttribute(E.ekey);
			if (ek)
				break;
			else
				target = target.parentNode;
		}
		if (!ek)
			return true;
		var eobj = E.evtData[ek];
		if (!eobj || !eobj[evtName])
			return true;
		
		if (eobj[evtName] && eobj[evtName].call(target, e) === false) {
			e.preventDefault();
			e.stopPropagation();
			return false;
		} else {}
	},
	bind : function (elm, ename, fn) {
		if( !elm )
			throw 'elm is null';
		var isSelfDef = false;
		var defEvts = ['swipe', 'swipeLeft', 'swipeRight', 'swipeUp', 'swipeDown', 'doubleTap', 'tap', 'longTap'];
		var i = defEvts.length - 1;
		for (; i >= 0; i--) {
			if (ename == defEvts[i]) {
				isSelfDef = true;
				break;
			}
		}
		if (isSelfDef) {
			var tempEid;
			var currentEkey = elm.getAttribute(E.ekey);
			if (!currentEkey) {
				tempEid = E.eid++;
				elm.setAttribute(E.ekey, tempEid);
			} else
				tempEid = currentEkey;
			if (!E.evtData[tempEid])
				E.evtData[tempEid] = {};
			E.evtData[tempEid][defEvts[i]] = fn;
		}
	}
};
(function (E) {
	if( !document.addEventListener )
		return ;
	
	var touch = {},
	touchTimeout;
	function parentIfText(node) {
		return 'tagName' in node ? node : node.parentNode;
	}
	function swipeDirection(x1, x2, y1, y2) {
		var xDelta = Math.abs(x1 - x2),
		yDelta = Math.abs(y1 - y2);
		if (xDelta >= yDelta) {
			return ( x1 - x2 > 0 ? 'Left' : 'Right');
		} else {
			return (y1 - y2 > 0 ? 'Up' : 'Down');
		}
	}
	var longTapDelay = 750;
	function longTap() {
		if (touch.last && (Date.now() - touch.last >= longTapDelay)) {
			//$(touch.target).trigger('longTap');
			E.trigger(null, touch.target, 'longTap');
			touch = {};
		}
	}
	document.addEventListener('DOMContentLoaded', function (e) {
		var body = document.body;
		body.addEventListener('touchstart', function (e) {
			var now = Date.now(),
			delta = now - (touch.last || now);
			touch.target = parentIfText(e.touches[0].target);
			touchTimeout && clearTimeout(touchTimeout);
			touch.x1 = e.touches[0].pageX;
			touch.y1 = e.touches[0].pageY;
			if (delta > 0 && delta <= 250)
				touch.isDoubleTap = true;
			touch.last = now;
			setTimeout(longTap, longTapDelay);
		}, false);
		
		body.addEventListener('touchmove', function (e) {
			touch.x2 = e.touches[0].pageX;
			touch.y2 = e.touches[0].pageY;
		}, false);
		
		body.addEventListener('touchend', function (e) {
			if (touch.isDoubleTap) {
				//$(touch.target).trigger('doubleTap');
				E.trigger(e, touch.target, 'doubleTap');
				touch = {};
			} else if (touch.x2 > 0 || touch.y2 > 0) {
				(Math.abs(touch.x1 - touch.x2) > 30 || Math.abs(touch.y1 - touch.y2) > 30) &&
				//$(touch.target).trigger('swipe') &&
				//$(touch.target).trigger('swipe' + (swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2)));
				E.trigger(e, touch.target, 'swipe');
				E.trigger(e, touch.target, 'swipe' + swipeDirection(touch.x1, touch.x2, touch.y1, touch.y2));
				touch.x1 = touch.x2 = touch.y1 = touch.y2 = touch.last = 0;
			} else if ('last' in touch) {
				touchTimeout = setTimeout(function () {
						touchTimeout = null;
						//$(touch.target).trigger('tap')
						E.trigger(e, touch.target, 'tap');
						touch = {};
					}, 250);
			}
		}, false);
		
		body.addEventListener('touchcancel', function () {
			touch = {}
			
		}, false);
	}, false);
})(E);


})();
